﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using Microsoft.Phone.Shell;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.UserData;
using System.Windows.Media.Imaging;
using LiveEduWizard.Views;
using System.Collections;

namespace LiveEduWizard
{
    public partial class MainPage : PhoneApplicationPage
    {


        // Constructor
        public MainPage()
        {
            InitializeComponent();
            InitMainBackground();
            InitApplicationBar();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            App.ViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ViewModel_PropertyChanged);
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            Panorama.Loaded += new RoutedEventHandler(Panorama_Loaded);

        }

        void Panorama_Loaded(object sender, RoutedEventArgs e)
        {
            // Set an active tab to the current day
            if (!String.Equals("agenda", UserSettings.SelectedTab) && Panorama.SelectedIndex == 0)
            {
                SwitchSelectedTab();
            }
            Panorama.Loaded -= Panorama_Loaded;
        }
        
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Load data for the ViewModel Items
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "ActiveAgenda":
                    Agenda.ScrollTo(App.ViewModel.ActiveAgenda);
                    break;
            }
        }

        private void InitApplicationBar()
        {
            ApplicationBar = new ApplicationBar();
            ApplicationBar.Mode = ApplicationBarMode.Default;
            ApplicationBar.Opacity = 0.8;
            ApplicationBar.IsVisible = true;
            ApplicationBar.IsMenuEnabled = true;
            InitApplicationBarButtons();
        }

        private void InitApplicationBarButtons()
        {

            // current icon button
            ApplicationBarIconButton currentButton = new ApplicationBarIconButton(ThemeHelper.CurrentButtonImage);
            currentButton.Text = ThemeHelper.CurrentButtonText;
            currentButton.Click += new EventHandler(currentButton_Click);
            ApplicationBar.Buttons.Add(currentButton);

            // switch icon button
            ApplicationBarIconButton switchButton = new ApplicationBarIconButton(ThemeHelper.SwitchButtonImage);
            switchButton.Text = ThemeHelper.SwitchButtonText;
            switchButton.Click += new EventHandler(switchButton_Click);
            ApplicationBar.Buttons.Add(switchButton);

            // filter icon button
            ApplicationBarIconButton filterButton = new ApplicationBarIconButton(ThemeHelper.FilterButtonImage);
            filterButton.Text = ThemeHelper.FilterButtonText;
            filterButton.Click += new EventHandler(filterButton_Click);
            ApplicationBar.Buttons.Add(filterButton);

            // settings icon button
            ApplicationBarIconButton settingsButton = new ApplicationBarIconButton(ThemeHelper.SettingsButtonImage);
            settingsButton.Text = ThemeHelper.SettingsButtonText;
            settingsButton.Click += new EventHandler(settingsButton_Click);
            ApplicationBar.Buttons.Add(settingsButton);

            // select week
            ApplicationBarMenuItem selectWeekButton = new ApplicationBarMenuItem();
            selectWeekButton.Text = ThemeHelper.SelectWeekButtonText;
            ApplicationBar.MenuItems.Add(selectWeekButton);
            selectWeekButton.Click += new EventHandler(selectWeekButton_Click);

            // reload button
            ApplicationBarMenuItem reloadButton = new ApplicationBarMenuItem();
            reloadButton.Text = ThemeHelper.ReloadButtonText;
            ApplicationBar.MenuItems.Add(reloadButton);
            reloadButton.Click += new EventHandler(reloadButton_Click);

            // about button
            ApplicationBarMenuItem aboutButton = new ApplicationBarMenuItem();
            aboutButton.Text = ThemeHelper.AboutButtonText;
            ApplicationBar.MenuItems.Add(aboutButton);
            aboutButton.Click += new EventHandler(aboutButton_Click);

        }

        public static void ShowProgressIndicator() 
        {
            App.ShowProgressPopup(false);
        }

        public static void HideProgressIndicator()
        {

            Deployment.Current.Dispatcher.BeginInvoke(() => { App.HideProgressPopup(); });
        }

        void currentButton_Click(object sender, EventArgs e)
        {
            if (Panorama.SelectedItem is TimetableView)
            {
                App.ViewModel.ResetActiveTimetable();
                
            }
            else if (Panorama.SelectedItem is AgendaView)
            {
                App.ViewModel.ResetActiveAgenda();
                Agenda.ScrollTo(App.ViewModel.ActiveAgenda);
            }
        }

        void switchButton_Click(object sender, EventArgs e)
        {
            SwitchSelectedTab();
        }

        private void SwitchSelectedTab()
        {
            if (Panorama.SelectedIndex == 0)
            {
                var day = DateTime.Now.DayOfWeek;

                if (day == DayOfWeek.Saturday || day == DayOfWeek.Sunday)
                    day = DayOfWeek.Monday;

                Panorama.DefaultItem = Panorama.Items[(int)day];
            }
            else
            {
                Panorama.DefaultItem = Panorama.Items[0];
            }
        }


        void filterButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/FilterPanel.xaml", UriKind.Relative));
        }

        void selectWeekButton_Click(object sender, EventArgs e)
        {
            MainPage.ShowProgressIndicator();
            NavigationService.Navigate(new Uri("/Views/WeekSelector.xaml", UriKind.Relative));
        }

        void settingsButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/SettingsPanel.xaml", UriKind.Relative));

        }

        void reloadButton_Click(object sender, EventArgs e)
        {
            App.ViewModel.LoadData();
        }

        void aboutButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AboutPanel.xaml", UriKind.Relative)); 
        }

        private void InitMainBackground()
        {

            ThemeHelper.SetMainBachground(MainBackground);
            ThemeHelper.UpdateMainBackground();
        }

        private void ContextMenuItem_Click(object sender, RoutedEventArgs e)
        {
            App.ViewModel.LoadData();
        }

    }
}